﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace ListOfTale
{
    /// <summary>
    /// Interaction logic for SumPieChart.xaml
    /// </summary>
    public partial class SumPieChart : Window
    {
        public SumPieChart()
        {
            InitializeComponent();
            for (int i = 1918; i <= 2012; i++)
            {
                StartYearCmb.Items.Add(i);
                EndYearCombobox.Items.Add(i);
            }
            DataTypeCmb.Items.Add("Тираж");
            DataTypeCmb.Items.Add("Издания");
            DataTypeCmb.SelectedIndex = 0;
            StartYearCmb.SelectedIndex = 0;
            EndYearCombobox.SelectedIndex = EndYearCombobox.Items.Count - 1;
        }
        public PieData[] BuildChartData(int startYear, int endYear)
        {

            if (DataTypeCmb.Text == "Тираж")
            {
                TalesDataSetTableAdapters.DataTable2TableAdapter dt = new TalesDataSetTableAdapters.DataTable2TableAdapter();
                var print = dt.GetPrintStat(startYear, endYear);                
                return print.GroupBy(ss => ss.Name).Select(r => new PieData() { Name = r.Key, Value = r.Sum(rr =>Convert.ToInt64( rr.Printing)) }).ToArray();
            }
            else
                if (DataTypeCmb.Text == "Издания")
                {
                    TalesDataSetTableAdapters.DataTable1TableAdapter dt = new TalesDataSetTableAdapters.DataTable1TableAdapter();
                    var print = dt.GetBookStat(startYear, endYear);
                    return print.GroupBy(ss => ss.Name).Select(r => new PieData() { Name = r.Key, Value = Convert.ToInt64(r.Sum(ss => Convert.ToInt64(ss.Books))) }).ToArray();
                }
                else
                {
                    throw new ApplicationException("нужно выбрать параметр в выпадающем списке");
                }
        }

        private void btnRefresh_Click(object sender, RoutedEventArgs e)
        {
            int startyear =Convert.ToInt32( StartYearCmb.Text);
            int endyear = Convert.ToInt32(EndYearCombobox.Text);
            var sers = BuildChartData(startyear,endyear);
            PieChart.ItemsSource = sers;          
        }
    }

    public class PieData
    {
        public string Name { get; set; }
        public long Value { get; set; }
    }    
}
